package com.biz.primus.ms.geo.dao.po;

import com.biz.primus.base.enums.CommonStatus;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.math.BigDecimal;

/**
 * @author JKLiues
 * @date 2017/11/8
 */
@Getter
@Setter
@MappedSuperclass
public class AbstractAreaWithCode {

    @Id
    private Integer id;

    @JsonIgnore
    @Column(length = 50, nullable = false)
    protected String code;

    @Column(length = 50)
    protected String name;

    @JsonIgnore
    @Column
    protected Integer idx;

    @JsonIgnore
    @Column
    protected Integer weight = 0;

    @JsonIgnore
    @Column(columnDefinition = "MEDIUMTEXT")
    protected String coordinate;

    @JsonIgnore
    @Column(columnDefinition = "decimal(9,6)")
    protected BigDecimal lat;

    @JsonIgnore
    @Column(columnDefinition = "decimal(9,6)")
    protected BigDecimal lon;

    @JsonIgnore
    @Column(length = 50)
    protected String baiduname;

    @JsonIgnore
    @Column(length = 255)
    protected String description;

    @JsonIgnore
    @Column
    protected Integer baiducode;

    @JsonIgnore
    @Column(length = 50)
    protected String post;

    @Column
    protected String prefix;

    @Column
    @Enumerated(EnumType.STRING)
    private CommonStatus status = CommonStatus.ENABLE;


    /**
     * double minX, double maxX, double minY, double maxY
     */
    @JsonIgnore
    public double[] getOutBox() {
        if (StringUtils.isNotBlank(coordinate)) {
            double[] result = {180d, -180d, 90d, -90d};
            try {
                String[] arr = coordinate.split("\\|");
                for (String s : arr) {
                    if (StringUtils.isNotBlank(s)) {
                        String[] parr = s.split(";");
                        for (String p : parr) {
                            int idx = p.indexOf(',');
                            double lon = Double.valueOf(p.substring(0, idx).trim());
                            double lat = Double.valueOf(p.substring(idx + 1).trim());
                            result[0] = Math.min(result[0], lon);
                            result[1] = Math.max(result[1], lon);
                            result[2] = Math.min(result[2], lat);
                            result[3] = Math.max(result[3], lat);
                        }
                    }
                }

            } catch (Exception e) {
            }
            return result;
        } else {
            return null;
        }
    }


}
